import requests
from bs4 import BeautifulSoup
import re

allUniv = []

#获取指定界面信息
def getHttpText(url):
    try:
        rsp=requests.get(url,timeout=30)
        rsp.raise_for_status()
        rsp.encoding="utf-8"
        return rsp.text
    except:
        return "Fail to obtain context"

#通过查找来获取指定信息
def findText(text):
    soup = BeautifulSoup(text,"html.parser")
    return soup.find_all(string=re.compile("百度"))

#通过匹配标签来获取相关信息
def fillList(text):
    soup = BeautifulSoup(text, "html.parser")
    data=soup.find_all('tr')
    print(data)

    i=1;
    for tr in data:
        ltd=tr.find_all('td')
        if len(ltd)==0:
            continue
        singleUniv=[]
        for td in ltd:
            la=td.find('a')
            if la is not None:
                if len(la)>0:
                    print("-------"+str(la.string)+"not none")
                    singleUniv.append(la.string)
            else:
                td=str(td.string).replace("\n","").replace("None","").strip()
                print("td:"+td)
                if td !='':
                    # print("-------"+str(len(td)))
                    singleUniv.append(td)
        print("第%d行信息:"%i+str(singleUniv))
        allUniv.append(singleUniv)
        i=i+1;

def printUnivList(num):
    print("{:^4}{:^10}{:^8}{:^8}".format("排名","学校","总分","层次"))
    for i in range(num):
        u = allUniv[i]
        print("{:^4}{:^10}{:^8}{:^8}".format(u[0],u[1],u[2],u[3]))
